#! /bin/sh

# This file is part of IVRE.
# Copyright 2011 - 2014 Pierre LALET <pierre.lalet@cea.fr>
#
# IVRE is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# IVRE is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License
# along with IVRE. If not, see <http://www.gnu.org/licenses/>.


# This is an equivalent of ivre runscansagent (the --sync part only)
# that do not need python.

RSYNC="rsync"
# Uncomment the following line to use TOR
#RSYNC="torify rsync"

MAINDIR="./agentsdata"

# [user@]host:path [[user@]host:path [...]]
AGENTS="user@host:path"

SLEEP="2"

function agent_path () {
    echo "${MAINDIR}/$(echo $1 | tr ':@' '__')"
}

# make directories
for a in $AGENTS ; do
    for d in input remoteinput remotecur remoteoutput ; do
	echo -p "$(agent_path $a)/${d}/"
    done
done

# sync loop
while true ; do
    for a in $AGENTS ; do
	${RSYNC} -a "$(agent_path $a)/input/" "$(agent_path $a)/remoteinput/"
	${RSYNC} --remove-source-files "$(agent_path $a)/input/" "${a}/input/"
	${RSYNC} --delete "${a}/input/" "$(agent_path $a)/remoteinput/" 
	${RSYNC} --delete "${a}/cur/" "$(agent_path $a)/remotecur/" 
	${RSYNC} --remove-source-files "${a}/output/" "$(agent_path $a)/remoteoutput/"
    done
    sleep ${SLEEP}
done
